package com.sanjin.controller;

import com.sanjin.annotations.SysLogOperation;
import com.sanjin.convert.AdminConvert;
import com.sanjin.entity.Admin;
import com.sanjin.form.AdminForm;
import com.sanjin.form.interfaces.Create;
import com.sanjin.form.interfaces.Update;
import com.sanjin.service.AdminService;
import com.sanjin.utils.JwtUtils;
import com.sanjin.utils.ResponseVoUtils;
import com.sanjin.vo.AdminVo;
import com.sanjin.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/5/6 18:34
 */
@Api(tags = "admin 相关接口")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private AdminConvert adminConvert;


    @Autowired
    private AdminService adminService;


    @ApiOperation(value = "获取admin信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = JwtUtils.HEADER, value = "用户令牌", paramType = "header")
    })
    @GetMapping("/list")
    public ResponseVo getAdminVo() {
        List<Admin> admins = adminService.queryAdmins();
        List<AdminVo> adminVos = adminConvert.admin2AdminVo(admins);
        return ResponseVoUtils.success(adminVos);
    }

    @PreAuthorize("hasRole('ROLE_SUPERUSER')")
    @SysLogOperation(value = "【添加admin用户】")
    @ApiOperation(value = "创建admin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = JwtUtils.HEADER, value = "用户令牌", paramType = "header")
    })
    @PostMapping("")
    public ResponseVo createAdmin(@RequestBody @Validated(Create.class) AdminForm adminForm) {
        Admin admin = adminConvert.adminForm2Admin(adminForm);
        Admin register = adminService.register(admin);
        return ResponseVoUtils.success();
    }

    @SysLogOperation(value = "【修改admin信息】")
    @ApiOperation(value = "修改admin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = JwtUtils.HEADER, value = "用户令牌", paramType = "header")
    })
    @PutMapping("")
    public ResponseVo updateAdmin(@RequestBody @Validated(Update.class) AdminForm adminForm) {
        Admin admin = adminConvert.adminForm2Admin(adminForm);
        Admin update = adminService.updateAdmin(admin);
        return ResponseVoUtils.success();
    }

    @SysLogOperation(value = "【删除admin信息】")
    @ApiOperation(value = "删除admin")
    @ApiImplicitParams({
            @ApiImplicitParam(name = JwtUtils.HEADER, value = "用户令牌", paramType = "header")
    })
    @DeleteMapping("")
    public ResponseVo deleteAdmin(@RequestBody List<Integer> ids) {
        adminService.deleteAdminsByIds(ids);
        return ResponseVoUtils.success();
    }
}
